<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Captcha_Entity extends CI_Model
{
    private $tablename = 'captcha';
    private $captcha_id = null;
    private $captcha_time = null;
    private $ip_address = null;
    private $word = null;
    
    public function setCaptcha_id($Captcha_id)
    {
        $this->captcha_id = $Captcha_id;
    }

    public function setCaptcha_time($Captcha_time)
    {
        $this->captcha_time = $Captcha_time;
    }

    public function setIp_address($Ip_address)
    {
        $this->ip_address = $Ip_address;
    }

    public function setWord($Word)
    {
        $this->word = $Word;
    }

    public function save()
    {
        $params = array();
        foreach ($this as $key => $val) {
            if ($key == 'tablename') continue;
            if ($val === null || $val === '') continue;
            $params[$key] = $val;
        }
                
        if (!isset($params['id'])) {
            if ($this->db->insert($this->tablename, $params)) {
                return $this->db->insert_id();
            } else {
                return 0;
            }
        } else {
            return $this->db->update($this->tablename, $params, array('id' => $params['id']));
        }
        
    }
    
    public function del($id)
    {
        
        return $this->db->delete($this->tablename, array('id' => $id));    
    }
    
    public function getCaptchaById($id)
    {
        $query = $this->db->get_where($this->tablename, array('id' => $id));
        if ($query->num_rows() > 0) {
            return $query->row();
        }
        return false;
    }
    
    public function getAllList()
    {
        $query = $this->db->get($this->tablename);
        return $query->result();
    }
    
    public function TotalCaptcha()
    {
        $query = $this->db->count_all_results('Captcha'); 
        return $query->result();
    } 
}       
        